# coding:utf-8
from PyQt5.QtWidgets import QWidget
from src.globle import *
import time
from qfluentwidgets import FluentIcon
from fuc_download.download_files import *

from fuc_desktop.fuc_software.software_repairs_ui import Ui_software_repaires

class software_repairs(Ui_software_repaires, QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi(self)
        self.sys_info.clicked.connect(self.get_system_info)
        self.backup_log.clicked.connect(self.get_backup_log)
        self.sys_operation_info.clicked.connect(self.get_sys_operation_info)



def software_repair():
    print_head(head=[
        ".......................修复系统源、软件商店相关报错.........................."
    ])
    print_menu_options(menu_options=[
        "修复系统更新系统源---如商店里软件较少",
        "更新源时提示GPG错误",
        "更新软件源时，提示等待报头一直为0、软件源无法加锁",
        "安装软件提示失败",
        "软件商店报错---如打开商店报0001-0005等代码、dbus服务异常"
    ])
    select_menu(menu_dict={
        '0': return_to_previous_menu,
        '1': systemUpdate,
        '2': softcenter_repair('soft_GPG'),
        '3': softcenter_repair('soft_lock'),
        '4': softcenter_repair('soft_install_fail'),
        '5': softcenter_repair('soft_wrong_code')
    })

def systemUpdate():
    os.system('cp /etc/apt/sources.list /etc/apt/sources.list.bak')
    print_head(head=[
        "...........原则上更新管理器会在开机几分钟内，顶多一小时自行向公司的源发请求更新源地址。............."
        "....................因最近系统升级，最好只用1....................."
    ])
    print_menu_options(menu_options=[
        "从公司那边更新一下源地址",
        "备份现有源",
        "还原刚备份的源",
        "修复V10",
        "修复V10-sp1",
        "修复V10-sp1-hwe",
        "修复V10-sp1-麒麟9006c",
        "修复V10-sp1-990"
    ])
    os.system('sudo apt-get update')
    select_menu(menu_dict={
        '0': return_to_previous_menu,
        '1': lambda: os.system('kylin-source-update'),
        '2': lambda: os.system('cp /etc/apt/sources.list /etc/apt/sources.list.bak'),
        '3': lambda: os.system('cp /etc/apt/sources.list.bak /etc/apt/sources.list'),
        '4': lambda: os.system('echo "deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.0 main restricted universe multiverse\ndeb http://archive.kylinos.cn/kylin/partner juniper main\n" >/etc/apt/sources.list'),
        '5': lambda: os.system('echo "deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main universe multiverse restricted\ndeb http://archive.kylinos.cn/kylin/partner 10.1 main\n" >/etc/apt/sources.list'),
        '6': lambda: os.system('echo "deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse\ndeb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-hwe main\ndeb http://archive.kylinos.cn/kylin/partner 10.1 main\n" >/etc/apt/sources.list'),
        '7': lambda: os.system('echo "deb https://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-kv main restricted universe multiverse\ndeb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin9a0/custom/partner/10_1-kirin9a0 default all\n" >/etc/apt/sources.list'),
        '8': lambda: os.system('echo "deb https://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-pv main restricted universe multiverse\ndeb https://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse\n" >/etc/apt/sources.list')
    })

    os.system('sudo apt-get update')

def softcenter_repair(software_repair_flag):
    if software_repair_flag == 'soft_GPG':
        #源GPG错误
        os.system('sudo mv /var/lib/apt/lists /var/lib/apt/list_' + str(int(time.time())))
        os.system('sudo mkdir -p /var/lib/apt/lists/partial')
        os.system('sudo apt clean')
        os.system('sudo apt update')
    elif software_repair_flag == 'soft_lock':
        #源报头为0
        os.system('sudo rm -rf /var/lib/apt/lists/')
        os.system('sudo apt update')
    elif software_repair_flag == 'soft_install_fail':
    #软件安装失败
        os.system('sudo apt install -f')
        os.system('sudo apt-get update --fix-missing')
        os.system('sudo apt autoremove')
    elif software_repair_flag == 'soft_wrong_code':
        #软件商店报错
        os.system('sudo apt update')
        os.system('sudo apt install -y kylin-software-center --reinstall')
        os.system('sudo rm /var/lib/dpkg/updates/')
        os.system('sudo rm -rf /var/lib/apt/lists/*')
        os.system('sudo rm /var/lib/dpkg/lock')
        os.system('sudo rm /var/lib/dpkg/lock-frontend')
        os.system('sudo rm /var/cache/apt/archives/lock')
        os.system('sudo dpkg --configure -a')
        os.system('sudo apt install -f')
        os.system('sudo apt update')